package cn.fl.custpay.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.custpay.dto.McrLoanRecDTO;
import cn.fl.custpay.service.IMcrLoanRecApiService;
import cn.fl.custpay.service.IMcrLoanRecService;
import cn.fl.custpay.transform.McrLoanRecVOTransform;
import cn.fl.custpay.vo.McrLoanRecVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.constant.facade.framework.constant.DictionaryConstant;

/**
 * @Description: 小贷贷款记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrLoanRecServiceImpl implements IMcrLoanRecService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrLoanRecService.class);

	@Autowired
	private IMcrLoanRecApiService mcrLoanRecApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Override
	public McrLoanRecVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrLoanRecDTO dto = null;//this.自定义ApiService.selectById(id);
		return McrLoanRecVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrLoanRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.mcrLoanRecApiService.add(McrLoanRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrLoanRecApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrLoanRecVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrLoanRecApiService.modify(McrLoanRecVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		Pager pager = this.mcrLoanRecApiService.searchListPage(page);
		List<McrLoanRecVO> list = McrLoanRecVOTransform.toVOList((List<McrLoanRecDTO>) pager.getResultList());
		//翻译一下、支付平台
		String payChlCd = PropertiesCacheUtils.getProperty(DictionaryConstant.PAY_CHL_CD);
		List<KeyValueVO> listDic = this.sysDictionaryApiService.getKvList(payChlCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		Map<String, String> kv = new HashMap<>();
		for (KeyValueVO keyValueVO : listDic) {
			kv.put(keyValueVO.getKey(), keyValueVO.getValue());
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (McrLoanRecVO vo : list) {
				vo.setPayTypeNm(kv.get(String.valueOf(vo.getPayType())));
				vo.setAmount(String.valueOf(DecimalUtils.convertFenToYuan(Long.valueOf(vo.getAmount()))));//分转元
			}
		}
		kv.clear();
		//交易类型
		String transStatusCd = PropertiesCacheUtils.getProperty(DictionaryConstant.TRANS_STATUS_CD);
		List<KeyValueVO> transStatusCdlistDic = this.sysDictionaryApiService.getKvList(transStatusCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		for (KeyValueVO keyValueVO : transStatusCdlistDic) {
			kv.put(keyValueVO.getKey(), keyValueVO.getValue());
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (McrLoanRecVO vo : list) {
				vo.setIsSucNm(kv.get(String.valueOf(vo.getIsSuc())));
			}
		}
		pager.setResultList(list);
		return pager;
	}
}
